Imports OMANE.Common
Imports OMANE.Common.UI
Imports System.Collections.ObjectModel



Public Class Project

    Dim _DataSet As Data.DataSet = New DataSet()


    Public ReadOnly Property IsShared As Boolean
        Get
            Return True
        End Get
    End Property

    Public ReadOnly Property DataSet As DataSet
        Get
            Return _DataSet
        End Get
    End Property


    Private _DataSourceList As DataSourceCollection

    Public ReadOnly Property DataSourceList As DataSourceCollection
        Get
            If _DataSourceList Is Nothing Then
                _DataSourceList = New DataSourceCollection()
                '_DataSourceList.Add(New IndicateurDataSource() With {.Name = "Indicateurs", .DataSourceName = "Indicateurs"})
            End If
            Return _DataSourceList
        End Get
    End Property


    Private _IndicateurList As ObjectModel.ObservableCollection(Of Indicateur)

    Public ReadOnly Property IndicateurList As ObjectModel.ObservableCollection(Of Indicateur)
        Get
            If _IndicateurList Is Nothing Then
                _IndicateurList = New ObjectModel.ObservableCollection(Of Indicateur)
            End If
            Return _IndicateurList
        End Get
    End Property

    Private _FilterList As ObjectModel.ObservableCollection(Of DataSourceFilter)
    Public ReadOnly Property FilterList As ObjectModel.ObservableCollection(Of DataSourceFilter)
        Get
            If _FilterList Is Nothing Then
                _FilterList = New ObjectModel.ObservableCollection(Of DataSourceFilter)
            End If
            Return _FilterList
        End Get
    End Property

    Dim _ressources As New Windows.ResourceDictionary
    Public ReadOnly Property Ressources As Windows.ResourceDictionary
        Get
            If _ressources.Count = 0 Then
                If IO.File.Exists("projectressources.xaml") Then
                    Dim reader = IO.File.OpenRead("projectressources.xaml")
                    _ressources = Windows.Markup.XamlReader.Load(reader)
                    reader.Close()
                    reader.Dispose()
                Else
                    _ressources.Add("StandardBlueVioletBrush", Windows.Media.Brushes.BlueViolet)
                End If
                Windows.Application.Current.Resources.MergedDictionaries.Add(_ressources)
            End If

            Return _ressources
        End Get
    End Property
    Public Sub SaveRessources()
        Dim writer = New IO.StreamWriter("projectressources.xaml")
        Windows.Markup.XamlWriter.Save(_ressources, writer)
        writer.Close()
        writer.Dispose()
    End Sub



    Private _disable = False

    Private Function DataSourceFilterString(ByVal f As DataSourceFilter) As String
        Dim r = TryCast(f.Value, DataRowView)

        Dim result = ""

        If r IsNot Nothing Then

            result = "(" & f.DataMember.Name & "="

            If f.DataMember.RuntimeType Is GetType(String) Then
                result = result & "'" & r("Member") & "'"

            Else
                result = result & CDbl(r("Member")).ToString(Globalization.CultureInfo.InvariantCulture)
            End If
        Else

            Dim rarray = TryCast(f.Value, IEnumerable)

            If rarray.Cast(Of DataRowView).Count() > 0 Then
                result = "(" & f.DataMember.Name & " IN ("

                If f.DataMember.RuntimeType Is GetType(String) Then
                    result = result & String.Join(",", rarray.Cast(Of DataRowView).Select(Function(e) "'" & e("Member") & "'").ToArray())

                Else
                    result = result & String.Join(",", rarray.Cast(Of DataRowView).Select(Function(e) CDbl(e("Member")).ToString(Globalization.CultureInfo.InvariantCulture)).ToArray())

                End If

                result = result & "))"

            End If
        End If

        Return result
    End Function
    Friend Sub UpdateDataSourceFilter(ByVal dataSourceFilter As DataSourceFilter)
        If _disable Then Return
        Dim q = From f In FilterList
                Where f.DataSource Is dataSourceFilter.DataSource _
                And f.Value IsNot Nothing _
                Select DataSourceFilterString(f)


        Dim filter = String.Join(" AND ", q.Where(Function(s) Not String.IsNullOrEmpty(s)).ToArray())

        Try
            dataSourceFilter.DataSource.Data.Columns("_Filter_").Expression = If(String.IsNullOrEmpty(filter), "1", filter)

        Catch ex As Exception
            MsgBox(ex.ToString(), MsgBoxStyle.Critical)
        End Try
        '_disable = True
        For Each f In FilterList
            If f.DataSource Is dataSourceFilter.DataSource Then
                'Dim si = f._owner.SelectedItem
                ' f._owner.ItemsSource = Nothing
                'f._owner.ItemsSource = f._data
                'f._owner.SelectedItem = si
            End If
        Next
        '_disable = False
    End Sub

    Private FilterRelations As New List(Of DataRelation)
    Sub AddFilterTable(ByVal relation As DataRelation)
        FilterRelations.Add(relation)

        Dim q = From r In FilterRelations
                Select "Parent(" & r.RelationName & ").IsSelected"

        relation.ChildTable.Columns("_Filter_").Expression = String.Join("*", q.ToArray())
    End Sub


End Class
